﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.IDAL;
using Register.DALFactory;
using Register.Command;

namespace Register.BLL
{
    /// <summary>
    /// des：数据库字段ID生成类
    /// author：吴不晓  王岩岩
    /// date：2012-06-23
    /// </summary>
    public class DataIDHelper
    {

        private static String typeNameKey = "IDictionaryDALKey";

        /// <summary>
        /// 获取记录的ID
        /// </summary>
        /// <param name="type">字典类型（即表字典的主键名）</param>
        /// <returns>记录的ID</returns>
        public static String GetDataID(String type)
        {
            try
            {
                IDictionaryDAL iDictionary = DALFactory<IDictionaryDAL>.Create(typeNameKey);

                String previousID = iDictionary.GetDataIDByType(type);
                String nextID = "";

                if (previousID.Length == 5)
                {
                    nextID = GetNextFiveNumber(previousID);
                }
                else if (previousID.Length == 13)
                {
                    nextID = GetNextFourteenNumber(previousID);
                }
                if (iDictionary.UpdateDataIDByType(type, nextID))
                {
                    return nextID;
                }
                else
                {
                    nextID = "字段ID更新出错";
                    return nextID;
                }
            }
            catch (Exception e)
            {
                LogRecordHelper.LogNote("ERROR", e.Message);
                throw e;
            }
        }

        /// <summary>
        ///生成五位编号，获取当前编号的下一个编号，如传入HP001返回HP002
        /// </summary>
        /// <param name="args">当前编号</param>
        /// <returns>下一个编号</returns>
        private static string GetNextFiveNumber(string args)
        {
            char c1 = args[2];
            char c2 = args[3];
            char c3 = args[4];
            int n1 = c1 - 48;
            int n2 = c2 - 48;
            int n3 = c3 - 48;
            int n = n1 * 100 + n2 * 10 + n3 + 1;
            char[] t = new Char[2];
            t[0] = args[0];
            t[1] = args[1];
            string q = new string(t);
            q = q + n.ToString();
            if (q.Length == 3)
            {
                String q1 = new string(t);
                char[] t2 = new char[1];
                t2[0] = args[4];
                // string t1 = new string(t2);
                q1 = q1 + "00" + n;
                return q1;
            }
            if (q.Length == 4)
            {
                String q1 = new string(t);
                char[] t2 = new char[2];
                t2[0] = args[3];
                t2[1] = args[4];
                string t1 = new string(t2);
                q1 = q1 + "0" + n;
                return q1;
            }
            return q;
        }

        /// <summary>
        /// 生成十四位编号，获取当前编号的下一个编号，如传入HP20120622001返回HP20120622002
        /// </summary>
        /// <param name="args">当前编号</param>
        /// <returns>下一个编号</returns>
       private static string GetNextFourteenNumber(String args)
        {
            string time = System.DateTime.Now.ToString("yyyy-MM-dd");
            string[] str = time.Split('-');

            if (str[1].Length == 1)
            {
                str[1] = "0" + str[1];
            }
            if (str[2].Length == 1)
            {
                str[2] = "0" + str[2];
            }
            string t1 = args.Substring(2, 4);
            string t2 = args.Substring(6, 2);
            string t3 = args.Substring(8, 2);
            if (t1.Equals(str[0]) && t2.Equals(str[1]) && t3.Equals(str[2]))
            {
                char c1 = args[10];
                char c2 = args[11];
                char c3 = args[12];
                int n1 = c1 - 48;
                int n2 = c2 - 48;
                int n3 = c3 - 48;
                int n = n1 * 100 + n2 * 10 + n3 + 1;
                string l = args.Substring(0, 10) + n;
                if (l.Length == 11)
                { return args.Substring(0, 10) + "00" + n; }
                else if (l.Length == 12)
                { return args.Substring(0, 10) + "0" + n; }
                else
                { return l; }
            }
            else
            {
                return args.Substring(0, 2) +str[0] + str[1] + str[2] + "001";
            }
        }
    }
}
